package com.example.sys.login

import androidx.lifecycle.viewModelScope
import com.example.lib_common.mvi.BaseViewModel
import com.example.lib_common.mvi.ResponseEntity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch
import java.util.*

class LoginViewModel :BaseViewModel<LoginRepository,LoginState>(){
    override fun createRepo(): LoginRepository {
        return LoginRepository()
    }

    override fun createState(): LoginState {
        return LoginState(ResponseEntity(0,"",LoginEntity()))
    }

    fun login(map: HashMap<String,Any>){
        mutableContainer.updateRequestState { copy(
            request = true
        ) }
        viewModelScope.launch (Dispatchers.IO){
            repo.requestSign(map).flowOn(Dispatchers.IO)
                .collect{
                    mutableContainer.updateRequestState {
                        copy(request = false,complete=true)
                    }
                    mutableContainer.updateUiState {
                        copy(entity=it)
                    }
                }
        }
    }
}